package com.lyl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyl.common.CustomException;
import com.lyl.common.R;
import com.lyl.entity.Category;
import com.lyl.entity.Dish;
import com.lyl.entity.Setmeal;
import com.lyl.mapper.CategoryMapper;
import com.lyl.mapper.DishMapper;
import com.lyl.service.CategoryService;
import com.lyl.service.DishService;
import com.lyl.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 李亚龙
 * @version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    /**
     * 根据id删除分类，删除之前需要进行判断菜品或者套餐分类是否管理其他菜品
     * @param ids
     */
    @Override
    public void remove(Long ids) {

        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        long dishcount = dishService.count(dishLambdaQueryWrapper);
        if (dishcount > 0){//有关联，抛出业务异常
            throw new CustomException("当前分类项关联了菜品，不能删除");
        }
        //查询当前分类是否关联套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, ids);
        long setmealcount = setmealService.count(setmealLambdaQueryWrapper);
        if (setmealcount > 0){//有关联抛出异常
            throw new CustomException("当前分类项关联了套餐，不能删除");
        }
        //正常删除分类
        super.removeById(ids);
    }
}
